<?php

namespace app\admin\controller;

/**
 * @title 装修案例
 *
 */
class Cases extends Common
{
    /**
     * @title 获取直播
     * @desc  {"0":"接口地址：http://api.huaxiaoujia.com/index/live/query","1":"请求方式：GET"}
     * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * @param {"name":"case_id","type":"int","required":false,"default":"","desc":"查询单条数据ID，传此参数以下参数将不可用"}
     * @param {"name":"page","type":"int","required":false,"default":"1","desc":"当前页数"}
     * @param {"name":"listRows","type":"int","required":false,"default":"10","desc":"获取条目数"}
     * @param {"name":"key","type":"string","required":false,"default":"","desc":"查询关键字,持模糊查询，暂不支持分词查询"}
     * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * @return {"name":"id","type":"int","required":true,"desc":"直播ID","level":1}
     * @return {"name":"title","type":"string","required":true,"desc":"标题","level":1}
     * @return {"name":"originalImg","type":"string","required":true,"desc":"封面图片","level":1}
     * @return {"name":"liveStatus","type":"string","required":true,"desc":"直播工段","level":1}
     * @return {"name":"village","type":"string","required":true,"desc":"小区名称","level":1}
     * @return {"name":"houseType","type":"string","required":true,"desc":"对应小区户型","level":1}
     * @return {"name":"size","type":"int","required":true,"desc":"房屋面积","level":1}
     * @return {"name":"typeName","type":"string","required":true,"desc":"户型名称","level":1}
     */
    public function query()
    {
        $db = model('Live');
        if (input('case_id')) {
            $data = $db->find(input('case_id'));
        } else {
            $page = input('page') > 0 ? input('page') : 1;
            $listRows = input('listRows') > 0 ? input('listRows') : 10;
            if (input('key')) {
                $db->where('title', 'like', "%" . input('key') . "%");
            }
            $field = "id,title,original_img as originalImg,live_status as liveStatus,village,housetype as houseType,size,type_name as typeName";
            $data = $db->field($field)->page($page, $listRows)->where("type = 0")->select();
        }
        $this->rest($data);
    }

    public function caseImage()
    {
        $db = model('Live');
        $page = input('page') > 0 ? input('page') : 1;
        $listRows = input('listRows') > 0 ? input('listRows') : 20;
        $field = "i.image_url as originalImg,i.w,i.h";
        $data = $db->alias("c")->field($field)->page($page, $listRows)->join("k_decoration_images i", "c.id = i.case_id")->where("type = 0 AND LENGTH(i.image_url) > 10")->select();
        $this->rest($data);
    }

    /**
     * @title 设计效果
     * @desc  {"0":"接口地址：http://api.huaxiaoujia.com/index/live/images","1":"请求方式：GET"}
     * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * @param {"name":"case_id","type":"int","required":true,"default":"","desc":"直播ID"}
     * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * @return {"name":"imageUrl","type":"string","required":true,"desc":"图片路径","level":1}
     */

    public function images()
    {
        if (input('case_id')) {
            $data = model('LiveImage')->field("image_url as imageUrl")->where('case_id', input('case_id'))->select();
            if ($data) {
                $this->rest($data);
            } else {
                $this->rest('图片不存在', 's004');
            }
        } else {
            $this->rest('参数错误', 's004');
        }
    }

    /**
     * @title 直播内容
     * @desc  {"0":"接口地址：http://api.huaxiaoujia.com/index/live/content","1":"请求方式：GET"}
     * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * @param {"name":"case_id","type":"int","required":true,"default":"","desc":"直播ID"}
     * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * @return {"name":"user","type":"array","required":true,"desc":"用户信息","level":1}
     * @return {"name":"images","type":"array","required":true,"desc":"图片集合","level":1}
     * @return {"name":"content","type":"string","required":true,"desc":"内容","level":1}
     * @return {"name":"type","type":"string","required":true,"desc":"工段","level":1}
     * @return {"name":"time","type":"string","required":true,"desc":"直播时间","level":1}
     */

    public function content()
    {
        if (input('case_id')) {
            $data = model('DecorationLive')->field("uid as user,images,content,type,time")->where('case_id', input('case_id'))->order("time", "desc")->select();
            if ($data) {
                $this->rest($data);
            } else {
                $this->rest('还没有直播内容');
            }
        } else {
            $this->rest('参数错误', 's004');
        }
    }

    /**
     * @title 添加直播
     * @desc  {"0":"接口地址：http://api.huaxiaoujia.com/index/goods/insert","1":"请求方式：POST"}
     * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * @param {"name":"title","type":"string","required":true,"default":"","desc":"标题","level":1}
     * @param {"name":"image","type":"string","required":true,"default":"","desc":"封面图片","level":1}
     * @param {"name":"village","type":"string","required":true,"default":"","desc":"小区名称","level":1}
     * @param {"name":"house_type","type":"string","required":true,"default":"","desc":"户型名称","level":1}
     * @param {"name":"area","type":"string","required":true,"default":"","desc":"户型面积","level":1}
     * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * @return {"name":"data","type":"json","required":false,"desc":"返回数据","level":1}
     * @return {"name":"code","type":"string","required":true,"desc":"状态码","level":1}
     * @return {"name":"message","type":"string","required":true,"desc":"提示信息","level":1}
     *
     */
    public function insert()
    {
        $input = input('post.data/a')['data'];
        if (!is_array($input)) {
            $this->rest($input, 's005', 200);
        } else {
            $data['title'] = $input['title'];
            $data['image'] = base64_to_img($input['image']);
            $data['village'] = $input['village'];
            $data['house_type'] = $input['house_type'];
            $data['area'] = $input['area'];
            $data['type'] = 0;
            $data['status'] = 1;
            $data['add_time'] = time();
            if (db('live')->insert($data)) {
                $this->rest('添加成功');
            } else {
                $this->rest('添加失败', "s005");
            }
        }
    }
}
